
#define F_CPU 16000000UL 
#define PRESCALER 8

#define TIMER45STEP (((F_CPU/PRESCALER)))
#define ANZAHL_IMPULSE 12
#define TEMPOMAT 1
#define ANZ_SERVOS 5
#define RECHTS 0
#define LINKS 1
#define AUS 0
#define AKTIV 1
#define ABBRUCH 2
#define TIMERRUECKLAUF 3
#define FEHLER 4
#define COUNT_UP 5
#define COUNT_DOWN 6
#define READY 7
#define false 0
#define true 1


#define TWIM_READ    1
#define TWIM_WRITE   0

#include <stdio.h> 
#include <stdlib.h> 
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>


int RpmSoll[2];
unsigned int StatusAbstandMessung;
volatile unsigned int StatusTimer1;
int Heading;


volatile uint16_t Sekunden;

volatile struct {
	uint8_t MessageComplete:1;
	uint8_t Busy:1;
	uint8_t PufferOverflow:1;
	uint8_t TimedOut:1;
	uint8_t TransmissionActive:1;
	uint8_t TransmissionComplete:1;
	uint8_t MessageValid:1;
	uint8_t Reserved4:1;
	uint8_t BytePuffer[32];
	uint8_t PufferSize;
	uint8_t MessageLength;
} Uart;

//volatile uint8_t UartBytePuffer[32];
//volatile uint8_t UartPufferSize;

/*volatile struct {
	int IstDrehzahl;
	int SollDrehzahl;
	unsigned int ICR;
	unsigned long Umdrehungen;
	uint8_t Messung;
	int8_t Richtung;
} Motor[2];
*/

uint8_t Servo[3];
uint8_t Motor[3];
uint8_t Licht[3];

//Funktionsdeklarationen
uint8_t SetSpeed2(void);
void DrehzahlBerechnung(void);
uint8_t SetLight(uint8_t Number, uint8_t Status);
uint8_t TWIM_Start (uint8_t address, uint8_t TWIM_Type);
uint8_t	TWIM_Write (uint8_t byte);
void	TWIM_Stop (void);
void UartInit(void);
//void DrehzahlBerechnung(unsigned int Periode_ms);
void InitDrehzahlMessung();
uint8_t SetSpeed(int seite, int speed);
void InitServos(int Anzahl, int BelegungsMaske, int Vorbelegung);
uint8_t SetServo(int Nr, int Pos);
int GetServoPos(int Nr);
int RPM(int Seite);
void UartSenden (char *s);
void SetRpmRight(int Rpm);
void SetRpmSpeed(int Seite, int Speed);

void wait_us(int mikroSec);
void UartPolling(void);
void Timer2Init(void);
int RpmIst(int Seite);
void SetRpm(unsigned int Seite);

uint8_t	TWIM_Init (uint32_t TWI_Bitrate);
//uint8_t TWIM_Start (uint8_t address, uint8_t TWIM_Type);
//void	TWIM_Stop (void);
//uint8_t	TWIM_Write (uint8_t byte);

uint8_t	TWIM_ReadAck (void);
uint8_t	TWIM_ReadNack (void);

void LED_Toggle(void);
